<template>
  <div class="index">
    <el-container class="box">
      <el-header class="el-header">
        <h1>华盛集团后台管理系统</h1>
        <el-dropdown trigger="click" @command="loginout">
          <span class="el-dropdown-link">
            {{ $store.state.userInfo.username
            }}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="一键换肤">一键换肤</el-dropdown-item>
            <el-dropdown-item command="退出登录">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            router
            unique-opened
            :default-active="$route.path"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#999"
          >
            <el-menu-item index="/home">
              <span slot="title">首页</span>
            </el-menu-item>
            <el-submenu
              :index="String(index)"
              v-for="(item, index) in menus"
              :key="item.id"
            >
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  v-for="item2 in item.children"
                  :key="item2.id"
                  :index="item2.url"
                  >{{ item2.title }}</el-menu-item
                >
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

    <el-dialog title="换肤图片" :visible.sync="isshow" @close="reset">
      <div class="img_box">
        <img
          @click="select(index)"
          :style="{
            border: index == ind ? '2px solid red' : 'none',
          }"
          v-for="(item, index) in imgArr"
          :key="item"
          :src="item"
          alt=""
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import caoshen from "../assets/img/1.jpg";
import yuanshenquanjiafu from "../assets/img/2.jpg";
import keqingbabala from "../assets/img/3.jpg";
export default {
  data() {
    return {
      menus: [],
      isshow: false,
      ind: -1,
      imgIndex: -1,
      imgArr: [caoshen, yuanshenquanjiafu, keqingbabala],
      arrTag: [true, true, true],
    };
  },
  mounted() {
    this.initMenus();
    let imgInfo = localStorage.getItem("imgInfo")
      ? JSON.parse(localStorage.getItem("imgInfo"))
      : null;
    if (imgInfo) {
      this.arrTag = imgInfo.arrTag;
      this.imgIndex = imgInfo.imgIndex;
      this.ind = imgInfo.imgIndex;
      document.body.style.cssText = `background: url(${
        this.imgArr[this.imgIndex]
      }) no-repeat; background-size: cover;`;
      document.querySelector("#app").style.cssText = `opacity: 0.85;`;
    }
  },
  methods: {
    initMenus() {
      let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      this.menus = userInfo.menus;
    },
    loginout(e) {
      if (e == "退出登录") {
        this.$store.dispatch("setUserInfo");
        this.$message.success("退出成功");
        this.$router.push("/login");
        localStorage.removeItem("imgInfo");
        document.body.style.cssText = `background: none`;
        document.querySelector("#app").style.cssText = `opacity: 1;`;
      } else {
        this.isshow = true;
      }
    },
    submit() {
      if (this.ind == -1) {
        document.body.style.cssText = `background: none;`;
        document.querySelector("#app").style.cssText = `opacity: 1;`;
        localStorage.removeItem("imgInfo");
        this.imgIndex = -1;
      } else {
        this.imgIndex = this.ind;
        document.body.style.cssText = `background: url(${
          this.imgArr[this.imgIndex]
        }) no-repeat; background-size: cover;`;
        document.querySelector("#app").style.cssText = `opacity: 0.85;`;
        localStorage.setItem(
          "imgInfo",
          JSON.stringify({
            imgIndex: this.imgIndex,
            arrTag: this.arrTag,
          })
        );
      }
      this.reset();
    },
    select(index) {
      if (this.arrTag[index]) {
        this.ind = index;
        this.arrTag = [true, true, true];
        this.arrTag[index] = false;
      } else {
        this.ind = -1;
        this.arrTag[index] = true;
      }
    },
    reset() {
      this.ind = this.imgIndex;
      this.arrTag = [true, true, true];
      this.arrTag[this.imgIndex] = false;
      this.isshow = false;
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  height: 80px !important;
  line-height: 80px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

body > .el-container {
  margin-bottom: 40px;
}

.box {
  height: 100%;
}

.index {
  width: 100%;
  height: 100%;
}

.img_box {
  display: flex;
}

.img_box img {
  width: 33%;
  height: 150px;
  margin-right: 10px;
}

.img_box img:last-child {
  margin-right: 0;
}

.is-active {
  background-color: rgba(0, 0, 0, 0.5) !important;
}

.el-dropdown {
  position: absolute;
  right: 30px;
  top: 0;
  cursor: pointer;
}

.el-header {
  position: relative;
}

.el-menu-vertical-demo {
  width: 100%;
}

.el-aside {
  overflow: hidden;
}
</style>    